package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityReceiptInfoEditBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.ReceiptEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.ReceiptInfoEditModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

public class ReceiptInfoEditActivity extends AppActivity {
    private ActivityReceiptInfoEditBinding binding;
    private boolean isAdd;
    @Inject
    ReceiptInfoEditModel viewModel;
    private ReceiptEntity receiptEntity;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context, ReceiptEntity receiptEntity) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("receiptEntity", receiptEntity);
        intent.putExtras(bundle);
        intent.setClass(context, ReceiptInfoEditActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void initView() {
        receiptEntity = (ReceiptEntity) getIntent().getSerializableExtra("receiptEntity");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_receipt_info_edit);
        if (receiptEntity != null) {
            binding.include.titleView.setText("编辑收款账号");
            isAdd = false;
            binding.sw.setChecked(receiptEntity.isDefault());
            binding.tvDelete.setVisibility(View.VISIBLE);
        } else {
            binding.include.titleView.setText("新增收款账号");
            isAdd = true;
            receiptEntity = new ReceiptEntity();
            binding.tvDelete.setVisibility(View.GONE);
        }
        binding.tvDelete.setOnClickListener(this);
        binding.itvBankNum.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.itvBankAcc.setNumberAndLetter("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        binding.itvBankName.setChinese();
        binding.setViewModel(receiptEntity);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.tvCommit.setOnClickListener(this);
        binding.sw.setOnCheckedChangeListener((compoundButton, b) -> {
            receiptEntity.setDefault(b);
        });
    }

    @Override
    protected void init() {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_commit:
                isNull();
                break;
            case R.id.tv_delete:
                showDialog(receiptEntity.getId());
                break;
        }
    }

    private void isNull() {
        if (TextUtils.isEmpty(binding.itvCompany.getContent())) {
            MToast.showToast(ReceiptInfoEditActivity.this, "请填写申请单位");
            return;
        } else if (TextUtils.isEmpty(binding.itvBankAcc.getContent())) {
            MToast.showToast(ReceiptInfoEditActivity.this, "请选填写银行账户");
            return;
        } else if (TextUtils.isEmpty(binding.itvBankName.getContent())) {
            MToast.showToast(ReceiptInfoEditActivity.this, "请填写开银户行");
            return;
        } else if (TextUtils.isEmpty(binding.itvBankNum.getContent())) {
            MToast.showToast(ReceiptInfoEditActivity.this, "请填写银行账号");
            return;
        }
        if (isAdd) {
            viewModel.receiptAdd(receiptEntity);
        } else {
            viewModel.receiptUpdate(receiptEntity);
        }
    }


    public void updateSucc() {
        MToast.showToast(this, "修改成功");
//        EventBus.getDefault().post(ShopEvent.make().setMessageTag(EventConstant.EVENT_RECEIPT_COMMIT));
        finish();
    }

    public void addSucc() {
        MToast.showToast(this, "新增成功");
//        EventBus.getDefault().post(ShopEvent.make().setMessageTag(EventConstant.EVENT_RECEIPT_COMMIT));
//        EventBus.getDefault().postSticky("REFRESH_COMPANY_INFO");
        finish();
    }

    private void showDialog(String id) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("删除收款账号");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText("是否删除收款账号");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口
//                deletePerson.put("id", id);
                viewModel.receiptDelete(id);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setTextColor(getResources().getColor(R.color.color_tab_one));
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
